<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>搭配 React | Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.3.3">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
    

        
    
    
    <link rel="next" href="../../docs/basics/ExampleTodoList.html" />
    
    
    <link rel="prev" href="../../docs/basics/DataFlow.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="2.5" data-basepath="../.." data-revision="Thu Mar 16 2017 23:41:56 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        自述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/introduction/index.html">
            
                
                    <a href="../../docs/introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="docs/introduction/Motivation.html">
            
                
                    <a href="../../docs/introduction/Motivation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        动机
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="docs/introduction/CoreConcepts.html">
            
                
                    <a href="../../docs/introduction/CoreConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        核心概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="docs/introduction/ThreePrinciples.html">
            
                
                    <a href="../../docs/introduction/ThreePrinciples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        三大原则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="docs/introduction/PriorArt.html">
            
                
                    <a href="../../docs/introduction/PriorArt.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        先前技术
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="docs/introduction/Ecosystem.html">
            
                
                    <a href="../../docs/introduction/Ecosystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        生态系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="docs/introduction/Examples.html">
            
                
                    <a href="../../docs/introduction/Examples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        示例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/basics/index.html">
            
                
                    <a href="../../docs/basics/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="docs/basics/Actions.html">
            
                
                    <a href="../../docs/basics/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="docs/basics/Reducers.html">
            
                
                    <a href="../../docs/basics/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="docs/basics/Store.html">
            
                
                    <a href="../../docs/basics/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="docs/basics/DataFlow.html">
            
                
                    <a href="../../docs/basics/DataFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        数据流
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.5" data-path="docs/basics/UsageWithReact.html">
            
                
                    <a href="../../docs/basics/UsageWithReact.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        搭配 React
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="docs/basics/ExampleTodoList.html">
            
                
                    <a href="../../docs/basics/ExampleTodoList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        示例：Todo List
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/advanced/index.html">
            
                
                    <a href="../../docs/advanced/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="docs/advanced/AsyncActions.html">
            
                
                    <a href="../../docs/advanced/AsyncActions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        异步 Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="docs/advanced/AsyncFlow.html">
            
                
                    <a href="../../docs/advanced/AsyncFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        异步数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="docs/advanced/Middleware.html">
            
                
                    <a href="../../docs/advanced/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Middleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="docs/advanced/UsageWithReactRouter.html">
            
                
                    <a href="../../docs/advanced/UsageWithReactRouter.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        搭配 React Router
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="docs/advanced/ExampleRedditAPI.html">
            
                
                    <a href="../../docs/advanced/ExampleRedditAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        示例：Reddit API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="docs/advanced/NextSteps.html">
            
                
                    <a href="../../docs/advanced/NextSteps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        下一步
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/recipes/index.html">
            
                
                    <a href="../../docs/recipes/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        技巧
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="docs/recipes/MigratingToRedux.html">
            
                
                    <a href="../../docs/recipes/MigratingToRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        迁移到 Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="docs/recipes/UsingObjectSpreadOperator.html">
            
                
                    <a href="../../docs/recipes/UsingObjectSpreadOperator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        使用对象展开运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="docs/recipes/ReducingBoilerplate.html">
            
                
                    <a href="../../docs/recipes/ReducingBoilerplate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        减少样板代码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="docs/recipes/ServerRendering.html">
            
                
                    <a href="../../docs/recipes/ServerRendering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        服务端渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="docs/recipes/WritingTests.html">
            
                
                    <a href="../../docs/recipes/WritingTests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        编写测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="docs/recipes/ComputingDerivedData.html">
            
                
                    <a href="../../docs/recipes/ComputingDerivedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        计算衍生数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="docs/recipes/ImplementingUndoHistory.html">
            
                
                    <a href="../../docs/recipes/ImplementingUndoHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        实现撤销重做
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="docs/recipes/IsolatingSubapps.html">
            
                
                    <a href="../../docs/recipes/IsolatingSubapps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        子应用隔离
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="docs/recipes/StructuringReducers.html">
            
                
                    <a href="../../docs/recipes/StructuringReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组织 Reducer
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.9.1" data-path="docs/recipes/reducers/PrerequisiteConcepts.html">
            
                
                    <a href="../../docs/recipes/reducers/PrerequisiteConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.1.</b>
                        
                        Reducer 基础概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.2" data-path="docs/recipes/reducers/BasicReducerStructure.html">
            
                
                    <a href="../../docs/recipes/reducers/BasicReducerStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.2.</b>
                        
                        Reducer 基础结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.3" data-path="docs/recipes/reducers/SplittingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/SplittingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.3.</b>
                        
                        Reducer 逻辑拆分
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.4" data-path="docs/recipes/reducers/RefactoringReducersExample.html">
            
                
                    <a href="../../docs/recipes/reducers/RefactoringReducersExample.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.4.</b>
                        
                        Reducer 重构示例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.5" data-path="docs/recipes/reducers/UsingCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/UsingCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.5.</b>
                        
                        `combineReducers` 用法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.6" data-path="docs/recipes/reducers/BeyondCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/BeyondCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.6.</b>
                        
                        `combineReducers` 进阶
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.7" data-path="docs/recipes/reducers/NormalizingStateShape.html">
            
                
                    <a href="../../docs/recipes/reducers/NormalizingStateShape.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.7.</b>
                        
                        State 范式化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.8" data-path="docs/recipes/reducers/UpdatingNormalizedData.html">
            
                
                    <a href="../../docs/recipes/reducers/UpdatingNormalizedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.8.</b>
                        
                        管理范式化数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.9" data-path="docs/recipes/reducers/ReusingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/ReusingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.9.</b>
                        
                        Reducer 逻辑复用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.10" data-path="docs/recipes/reducers/ImmutableUpdatePatterns.html">
            
                
                    <a href="../../docs/recipes/reducers/ImmutableUpdatePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.10.</b>
                        
                        不可变更新模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.11" data-path="docs/recipes/reducers/InitializingState.html">
            
                
                    <a href="../../docs/recipes/reducers/InitializingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.11.</b>
                        
                        初始化 State
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/FAQ.html">
            
                
                    <a href="../../docs/FAQ.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        常见问题
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="docs/faq/General.html">
            
                
                    <a href="../../docs/faq/General.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        综合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="docs/faq/Reducers.html">
            
                
                    <a href="../../docs/faq/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="docs/faq/OrganizingState.html">
            
                
                    <a href="../../docs/faq/OrganizingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        组织 State
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="docs/faq/StoreSetup.html">
            
                
                    <a href="../../docs/faq/StoreSetup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        创建 Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="docs/faq/Actions.html">
            
                
                    <a href="../../docs/faq/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="docs/faq/CodeStructure.html">
            
                
                    <a href="../../docs/faq/CodeStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        代码结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="docs/faq/Performance.html">
            
                
                    <a href="../../docs/faq/Performance.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        性能
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="docs/faq/ReactRedux.html">
            
                
                    <a href="../../docs/faq/ReactRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        React Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="docs/faq/Miscellaneous.html">
            
                
                    <a href="../../docs/faq/Miscellaneous.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        其它
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/Troubleshooting.html">
            
                
                    <a href="../../docs/Troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排错
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/Glossary.html">
            
                
                    <a href="../../docs/Glossary.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        词汇表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/api/index.html">
            
                
                    <a href="../../docs/api/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        API 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="docs/api/createStore.html">
            
                
                    <a href="../../docs/api/createStore.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        createStore
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="docs/api/Store.html">
            
                
                    <a href="../../docs/api/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="docs/api/combineReducers.html">
            
                
                    <a href="../../docs/api/combineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        combineReducers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="docs/api/applyMiddleware.html">
            
                
                    <a href="../../docs/api/applyMiddleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        applyMiddleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="docs/api/bindActionCreators.html">
            
                
                    <a href="../../docs/api/bindActionCreators.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        bindActionCreators
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="docs/api/compose.html">
            
                
                    <a href="../../docs/api/compose.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        compose
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/react-redux/index.html">
            
                
                    <a href="../../docs/react-redux/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        react-redux 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="docs/react-redux/api.html">
            
                
                    <a href="../../docs/react-redux/api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="docs/react-redux/troubleshooting.html">
            
                
                    <a href="../../docs/react-redux/troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        排错
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" >
            
                
                    <a target="_blank" href="https:/github.com/react-guide/redux-tutorial-cn">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        redux-tutorial
                    </a>
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <a id="edit-link" href="https://github.com/camsong/redux-in-chinese/tree/master/docs/basics/UsageWithReact.md" class="btn fa fa-edit pull-left">&#xA0;&#xA0;&#x5F00;&#x59CB;&#x7EA0;&#x9519;</a><h1 id="%E6%90%AD%E9%85%8D-react">&#x642D;&#x914D; React</h1>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x518D;&#x5F3A;&#x8C03;&#x4E00;&#x4E0B;&#xFF1A;Redux &#x548C; React &#x4E4B;&#x95F4;&#x6CA1;&#x6709;&#x5173;&#x7CFB;&#x3002;Redux &#x652F;&#x6301; React&#x3001;Angular&#x3001;Ember&#x3001;jQuery &#x751A;&#x81F3;&#x7EAF; JavaScript&#x3002;</p>
<p>&#x5C3D;&#x7BA1;&#x5982;&#x6B64;&#xFF0C;Redux &#x8FD8;&#x662F;&#x548C; <a href="http://facebook.github.io/react/" target="_blank">React</a> &#x548C; <a href="https://github.com/dekujs/deku" target="_blank">Deku</a> &#x8FD9;&#x7C7B;&#x6846;&#x67B6;&#x642D;&#x914D;&#x8D77;&#x6765;&#x7528;&#x6700;&#x597D;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x7C7B;&#x6846;&#x67B6;&#x5141;&#x8BB8;&#x4F60;&#x4EE5; state &#x51FD;&#x6570;&#x7684;&#x5F62;&#x5F0F;&#x6765;&#x63CF;&#x8FF0;&#x754C;&#x9762;&#xFF0C;Redux &#x901A;&#x8FC7; action &#x7684;&#x5F62;&#x5F0F;&#x6765;&#x53D1;&#x8D77; state &#x53D8;&#x5316;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x4F7F;&#x7528; React &#x6765;&#x5F00;&#x53D1;&#x4E00;&#x4E2A; todo &#x4EFB;&#x52A1;&#x7BA1;&#x7406;&#x5E94;&#x7528;&#x3002;</p>
<h2 id="%E5%AE%89%E8%A3%85-react-redux">&#x5B89;&#x88C5; React Redux</h2>
<p>Redux &#x9ED8;&#x8BA4;&#x5E76;&#x4E0D;&#x5305;&#x542B; <a href="https://github.com/reactjs/react-redux" target="_blank">React &#x7ED1;&#x5B9A;&#x5E93;</a>&#xFF0C;&#x9700;&#x8981;&#x5355;&#x72EC;&#x5B89;&#x88C5;&#x3002;</p>
<pre><code>npm install <span class="token operator">--</span>save react<span class="token operator">-</span>redux
</code></pre><h2 id="%E5%AE%B9%E5%99%A8%E7%BB%84%E4%BB%B6%EF%BC%88smart-container-components%EF%BC%89%E5%92%8C%E5%B1%95%E7%A4%BA%E7%BB%84%E4%BB%B6%EF%BC%88dumb-presentational-components%EF%BC%89">&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#xFF08;Smart/Container Components&#xFF09;&#x548C;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#xFF08;Dumb/Presentational Components&#xFF09;</h2>
<p>Redux &#x7684; React &#x7ED1;&#x5B9A;&#x5E93;&#x662F;&#x57FA;&#x4E8E; <a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0" target="_blank">&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x548C;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x76F8;&#x5206;&#x79BB;</a> &#x7684;&#x5F00;&#x53D1;&#x601D;&#x60F3;&#x3002;&#x6240;&#x4EE5;&#x5EFA;&#x8BAE;&#x5148;&#x8BFB;&#x5B8C;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#x518D;&#x56DE;&#x6765;&#x7EE7;&#x7EED;&#x5B66;&#x4E60;&#x3002;</p>
<p>&#x5DF2;&#x7ECF;&#x8BFB;&#x5B8C;&#x4E86;&#xFF1F;&#x90A3;&#x8BA9;&#x6211;&#x4EEC;&#x518D;&#x603B;&#x7ED3;&#x4E00;&#x4E0B;&#x4E0D;&#x540C;&#x70B9;&#xFF1A;</p>
<table>
    <thead>
        <tr>
            <th></th>
            <th scope="col" style="text-align:left">&#x5C55;&#x793A;&#x7EC4;&#x4EF6;</th>
            <th scope="col" style="text-align:left">&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;</th>
        </tr>
    </thead>
    <tbody>
        <tr>
          <th scope="row" style="text-align:right">&#x4F5C;&#x7528;</th>
          <td>&#x63CF;&#x8FF0;&#x5982;&#x4F55;&#x5C55;&#x73B0;&#xFF08;&#x9AA8;&#x67B6;&#x3001;&#x6837;&#x5F0F;&#xFF09;</td>
          <td>&#x63CF;&#x8FF0;&#x5982;&#x4F55;&#x8FD0;&#x884C;&#xFF08;&#x6570;&#x636E;&#x83B7;&#x53D6;&#x3001;&#x72B6;&#x6001;&#x66F4;&#x65B0;&#xFF09;</td>
        </tr>
        <tr>
          <th scope="row" style="text-align:right">&#x76F4;&#x63A5;&#x4F7F;&#x7528; Redux</th>
          <td>&#x5426;
          </td><td>&#x662F;
        </td></tr>
        <tr>
          <th scope="row" style="text-align:right">&#x6570;&#x636E;&#x6765;&#x6E90;</th>
          <td>props</td>
          <td>&#x76D1;&#x542C; Redux state</td>
        </tr>
        <tr>
          <th scope="row" style="text-align:right">&#x6570;&#x636E;&#x4FEE;&#x6539;</th>
          <td>&#x4ECE; props &#x8C03;&#x7528;&#x56DE;&#x8C03;&#x51FD;&#x6570;</td>
          <td>&#x5411; Redux &#x6D3E;&#x53D1; actions</td>
        </tr>
        <tr>
          <th scope="row" style="text-align:right">&#x8C03;&#x7528;&#x65B9;&#x5F0F;</th>
          <td>&#x624B;&#x52A8;</td>
          <td>&#x901A;&#x5E38;&#x7531; React Redux &#x751F;&#x6210;</td>
        </tr>
    </tbody>
</table>

<p>&#x5927;&#x90E8;&#x5206;&#x7684;&#x7EC4;&#x4EF6;&#x90FD;&#x5E94;&#x8BE5;&#x662F;&#x5C55;&#x793A;&#x578B;&#x7684;&#xFF0C;&#x4F46;&#x4E00;&#x822C;&#x9700;&#x8981;&#x5C11;&#x6570;&#x7684;&#x51E0;&#x4E2A;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x628A;&#x5B83;&#x4EEC;&#x548C; Redux store &#x8FDE;&#x63A5;&#x8D77;&#x6765;&#x3002;</p>
<p>&#x6280;&#x672F;&#x4E0A;&#x8BB2;&#x4F60;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528; <code>store<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x6765;&#x7F16;&#x5199;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x3002;&#x4F46;&#x4E0D;&#x5EFA;&#x8BAE;&#x8FD9;&#x4E48;&#x505A;&#x56E0;&#x4E3A;&#x5C31;&#x65E0;&#x6CD5;&#x4F7F;&#x7528; React Redux &#x5E26;&#x6765;&#x7684;&#x6027;&#x80FD;&#x4F18;&#x5316;&#x3002;&#x4E5F;&#x56E0;&#x6B64;&#xFF0C;&#x4E0D;&#x8981;&#x624B;&#x5199;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#xFF0C;&#x90FD;&#x662F;&#x4F7F;&#x7528; React Redux &#x7684; <code><span class="token function">connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x65B9;&#x6CD5;&#x6765;&#x751F;&#x6210;&#xFF0C;&#x540E;&#x9762;&#x4F1A;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#x3002;</p>
<h2 id="%E8%AE%BE%E8%AE%A1%E7%BB%84%E4%BB%B6%E5%B1%82%E6%AC%A1%E7%BB%93%E6%9E%84">&#x8BBE;&#x8BA1;&#x7EC4;&#x4EF6;&#x5C42;&#x6B21;&#x7ED3;&#x6784;</h2>
<p>&#x8FD8;&#x8BB0;&#x5F97;&#x5F53;&#x521D;&#x5982;&#x4F55; <a href="Reducers.html">&#x8BBE;&#x8BA1; state &#x6839;&#x5BF9;&#x8C61;&#x7684;&#x7ED3;&#x6784;</a> &#x5417;&#xFF1F;&#x73B0;&#x5728;&#x5C31;&#x8981;&#x5B9A;&#x4E49;&#x4E0E;&#x5B83;&#x5339;&#x914D;&#x7684;&#x754C;&#x9762;&#x7684;&#x5C42;&#x6B21;&#x7ED3;&#x6784;&#x3002;&#x5176;&#x5B9E;&#x8FD9;&#x4E0D;&#x662F; Redux &#x76F8;&#x5173;&#x7684;&#x5DE5;&#x4F5C;&#xFF0C;<a href="https://facebook.github.io/react/docs/thinking-in-react.html" target="_blank">React &#x5F00;&#x53D1;&#x601D;&#x60F3;</a>&#x5728;&#x8FD9;&#x65B9;&#x9762;&#x89E3;&#x91CA;&#x7684;&#x975E;&#x5E38;&#x68D2;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x7684;&#x6982;&#x8981;&#x8BBE;&#x8BA1;&#x5F88;&#x7B80;&#x5355;&#x3002;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x663E;&#x793A;&#x4E00;&#x4E2A; todo &#x9879;&#x7684;&#x5217;&#x8868;&#x3002;&#x4E00;&#x4E2A; todo &#x9879;&#x88AB;&#x70B9;&#x51FB;&#x540E;&#xFF0C;&#x4F1A;&#x589E;&#x52A0;&#x4E00;&#x6761;&#x5220;&#x9664;&#x7EBF;&#x5E76;&#x6807;&#x8BB0; completed&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x663E;&#x793A;&#x7528;&#x6237;&#x65B0;&#x589E;&#x4E00;&#x4E2A; todo &#x5B57;&#x6BB5;&#x3002;&#x5728; footer &#x91CC;&#x663E;&#x793A;&#x4E00;&#x4E2A;&#x53EF;&#x5207;&#x6362;&#x7684;&#x663E;&#x793A;&#x5168;&#x90E8;/&#x53EA;&#x663E;&#x793A; completed &#x7684;/&#x53EA;&#x663E;&#x793A; incompleted &#x7684; todos&#x3002;</p>
<h3 id="%E5%B1%95%E7%A4%BA%E7%BB%84%E4%BB%B6">&#x5C55;&#x793A;&#x7EC4;&#x4EF6;</h3>
<p>&#x4EE5;&#x4E0B;&#x7684;&#x8FD9;&#x4E9B;&#x7EC4;&#x4EF6;&#xFF08;&#x548C;&#x5B83;&#x4EEC;&#x7684; props &#xFF09;&#x5C31;&#x662F;&#x4ECE;&#x8FD9;&#x4E2A;&#x8BBE;&#x8BA1;&#x91CC;&#x6765;&#x7684;&#xFF1A;</p>
<ul>
<li><strong><code>TodoList</code></strong> &#x7528;&#x4E8E;&#x663E;&#x793A; todos &#x5217;&#x8868;&#x3002;<ul>
<li><code>todos<span class="token punctuation">:</span> Array</code> &#x4EE5; <code><span class="token punctuation">{</span> text<span class="token punctuation">,</span> completed <span class="token punctuation">}</span></code> &#x5F62;&#x5F0F;&#x663E;&#x793A;&#x7684; todo &#x9879;&#x6570;&#x7EC4;&#x3002;</li>
<li><code><span class="token function">onTodoClick</span><span class="token punctuation">(</span>index<span class="token punctuation">:</span> number<span class="token punctuation">)</span></code> &#x5F53; todo &#x9879;&#x88AB;&#x70B9;&#x51FB;&#x65F6;&#x8C03;&#x7528;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</li>
</ul>
</li>
<li><strong><code>Todo</code></strong> &#x4E00;&#x4E2A; todo &#x9879;&#x3002;<ul>
<li><code>text<span class="token punctuation">:</span> string</code> &#x663E;&#x793A;&#x7684;&#x6587;&#x672C;&#x5185;&#x5BB9;&#x3002;</li>
<li><code>completed<span class="token punctuation">:</span> boolean</code> todo &#x9879;&#x662F;&#x5426;&#x663E;&#x793A;&#x5220;&#x9664;&#x7EBF;&#x3002;</li>
<li><code><span class="token function">onClick</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x5F53; todo &#x9879;&#x88AB;&#x70B9;&#x51FB;&#x65F6;&#x8C03;&#x7528;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</li>
</ul>
</li>
<li><strong><code>Link</code></strong> &#x5E26;&#x6709; callback &#x56DE;&#x8C03;&#x529F;&#x80FD;&#x7684;&#x94FE;&#x63A5;<ul>
<li><code><span class="token function">onClick</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x5F53;&#x70B9;&#x51FB;&#x94FE;&#x63A5;&#x65F6;&#x4F1A;&#x89E6;&#x53D1;</li>
</ul>
</li>
<li><strong><code>Footer</code></strong> &#x4E00;&#x4E2A;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x6539;&#x53D8;&#x53EF;&#x89C1; todo &#x8FC7;&#x6EE4;&#x5668;&#x7684;&#x7EC4;&#x4EF6;&#x3002;</li>
<li><strong><code>App</code></strong> &#x6839;&#x7EC4;&#x4EF6;&#xFF0C;&#x6E32;&#x67D3;&#x4F59;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x5185;&#x5BB9;&#x3002;</li>
</ul>
<p>&#x8FD9;&#x4E9B;&#x7EC4;&#x4EF6;&#x53EA;&#x5B9A;&#x4E49;&#x5916;&#x89C2;&#x5E76;&#x4E0D;&#x5173;&#x5FC3;&#x6570;&#x636E;&#x6765;&#x6E90;&#x548C;&#x5982;&#x4F55;&#x6539;&#x53D8;&#x3002;&#x4F20;&#x5165;&#x4EC0;&#x4E48;&#x5C31;&#x6E32;&#x67D3;&#x4EC0;&#x4E48;&#x3002;&#x5982;&#x679C;&#x4F60;&#x628A;&#x4EE3;&#x7801;&#x4ECE; Redux &#x8FC1;&#x79FB;&#x5230;&#x522B;&#x7684;&#x67B6;&#x6784;&#xFF0C;&#x8FD9;&#x4E9B;&#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x4E0D;&#x505A;&#x4EFB;&#x4F55;&#x6539;&#x52A8;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x3002;&#x5B83;&#x4EEC;&#x5E76;&#x4E0D;&#x4F9D;&#x8D56;&#x4E8E; Redux&#x3002;</p>
<h3 id="%E5%AE%B9%E5%99%A8%E7%BB%84%E4%BB%B6">&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;</h3>
<p>&#x8FD8;&#x9700;&#x8981;&#x4E00;&#x4E9B;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x6765;&#x628A;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x8FDE;&#x63A5;&#x5230; Redux&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5C55;&#x793A;&#x578B;&#x7684; <code>TodoList</code> &#x7EC4;&#x4EF6;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x7C7B;&#x4F3C; <code>VisibleTodoList</code> &#x7684;&#x5BB9;&#x5668;&#x6765;&#x76D1;&#x542C; Redux store &#x53D8;&#x5316;&#x5E76;&#x5904;&#x7406;&#x5982;&#x4F55;&#x8FC7;&#x6EE4;&#x51FA;&#x8981;&#x663E;&#x793A;&#x7684;&#x6570;&#x636E;&#x3002;&#x4E3A;&#x4E86;&#x5B9E;&#x73B0;&#x72B6;&#x6001;&#x8FC7;&#x6EE4;&#xFF0C;&#x9700;&#x8981;&#x5B9E;&#x73B0; <code>FilterLink</code> &#x7684;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x6765;&#x6E32;&#x67D3; <code>Link</code> &#x5E76;&#x5728;&#x70B9;&#x51FB;&#x65F6;&#x89E6;&#x53D1;&#x5BF9;&#x5E94;&#x7684; action&#xFF1A;</p>
<ul>
<li><strong><code>VisibleTodoList</code></strong> &#x6839;&#x636E;&#x5F53;&#x524D;&#x663E;&#x793A;&#x7684;&#x72B6;&#x6001;&#x6765;&#x5BF9; todo &#x5217;&#x8868;&#x8FDB;&#x884C;&#x8FC7;&#x6EE4;&#xFF0C;&#x5E76;&#x6E32;&#x67D3; <code>TodoList</code>&#x3002;</li>
<li><strong><code>FilterLink</code></strong> &#x5F97;&#x5230;&#x5F53;&#x524D;&#x8FC7;&#x6EE4;&#x5668;&#x5E76;&#x6E32;&#x67D3; <code>Link</code>&#x3002;<ul>
<li><code>filter<span class="token punctuation">:</span> string</code> &#x5C31;&#x662F;&#x5F53;&#x524D;&#x8FC7;&#x6EE4;&#x7684;&#x72B6;&#x6001;</li>
</ul>
</li>
</ul>
<h3 id="%E5%85%B6%E5%AE%83%E7%BB%84%E4%BB%B6">&#x5176;&#x5B83;&#x7EC4;&#x4EF6;</h3>
<p>&#x6709;&#x65F6;&#x5F88;&#x96BE;&#x5206;&#x6E05;&#x5230;&#x5E95;&#x8BE5;&#x4F7F;&#x7528;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x8FD8;&#x662F;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6709;&#x65F6;&#x8868;&#x5355;&#x548C;&#x51FD;&#x6570;&#x4E25;&#x91CD;&#x8026;&#x5408;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x5982;&#x8FD9;&#x4E2A;&#x5C0F;&#x7684;&#x7EC4;&#x4EF6;&#xFF1A;</p>
<ul>
<li><strong><code>AddTodo</code></strong> &#x542B;&#x6709;&#x201C;Add&#x201D;&#x6309;&#x94AE;&#x7684;&#x8F93;&#x5165;&#x6846;</li>
</ul>
<p>&#x6280;&#x672F;&#x4E0A;&#x8BB2;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x5206;&#x6210;&#x4E24;&#x4E2A;&#x7EC4;&#x4EF6;&#xFF0C;&#x4F46;&#x4E00;&#x5F00;&#x59CB;&#x5C31;&#x8FD9;&#x4E48;&#x505A;&#x6709;&#x70B9;&#x65E9;&#x3002;&#x5728;&#x4E00;&#x4E9B;&#x975E;&#x5E38;&#x5C0F;&#x7684;&#x7EC4;&#x4EF6;&#x91CC;&#x6DF7;&#x7528;&#x5BB9;&#x5668;&#x548C;&#x5C55;&#x793A;&#x662F;&#x53EF;&#x4EE5;&#x7684;&#x3002;&#x5F53;&#x4E1A;&#x52A1;&#x53D8;&#x590D;&#x6742;&#x540E;&#xFF0C;&#x5982;&#x4F55;&#x62C6;&#x5206;&#x5C31;&#x5F88;&#x660E;&#x663E;&#x4E86;&#x3002;&#x6240;&#x4EE5;&#x73B0;&#x5728;&#x5C31;&#x4F7F;&#x7528;&#x6DF7;&#x5408;&#x578B;&#x7684;&#x4E2D;&#x3002;</p>
<h2 id="%E7%BB%84%E4%BB%B6%E7%BC%96%E7%A0%81">&#x7EC4;&#x4EF6;&#x7F16;&#x7801;</h2>
<p>&#x7EC8;&#x4E8E;&#x5F00;&#x59CB;&#x5F00;&#x53D1;&#x7EC4;&#x4EF6;&#x4E86;&#xFF01;&#x5148;&#x505A;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#xFF0C;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x5148;&#x4E0D;&#x8003;&#x8651; Redux&#x3002;</p>
<h3 id="%E5%B1%95%E7%A4%BA%E7%BB%84%E4%BB%B6">&#x5C55;&#x793A;&#x7EC4;&#x4EF6;</h3>
<p>&#x5B83;&#x4EEC;&#x53EA;&#x662F;&#x666E;&#x901A;&#x7684; React &#x7EC4;&#x4EF6;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x4F1A;&#x8BE6;&#x7EC6;&#x89E3;&#x91CA;&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5F0F;&#x65E0;&#x72B6;&#x6001;&#x7EC4;&#x4EF6;&#x9664;&#x975E;&#x9700;&#x8981;&#x672C;&#x5730; state &#x6216;&#x751F;&#x547D;&#x5468;&#x671F;&#x51FD;&#x6570;&#x7684;&#x573A;&#x666F;&#x3002;&#x8FD9;&#x5E76;&#x4E0D;&#x662F;&#x8BF4;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x5FC5;&#x987B;&#x662F;&#x51FD;&#x6570; -- &#x53EA;&#x662F;&#x56E0;&#x4E3A;&#x8FD9;&#x6837;&#x505A;&#x5BB9;&#x6613;&#x4E9B;&#x3002;&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x672C;&#x5730; state&#xFF0C;&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;&#xFF0C;&#x6216;&#x8005;&#x6027;&#x80FD;&#x4F18;&#x5316;&#xFF0C;&#x53EF;&#x4EE5;&#x6240;&#x5B83;&#x4EEC;&#x8F6C;&#x6210; class&#x3002;</p>
<h4 id="-components-todo-js"><code>components<span class="token operator">/</span>Todo<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> PropTypes <span class="token punctuation">}</span> from <span class="token string">&apos;react&apos;</span>

<span class="token keyword">const</span> Todo <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> onClick<span class="token punctuation">,</span> completed<span class="token punctuation">,</span> text <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>li
    onClick<span class="token operator">=</span><span class="token punctuation">{</span>onClick<span class="token punctuation">}</span>
    style<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span>
      textDecoration<span class="token punctuation">:</span> completed <span class="token operator">?</span> <span class="token string">&apos;line-through&apos;</span> <span class="token punctuation">:</span> <span class="token string">&apos;none&apos;</span>
    <span class="token punctuation">}</span><span class="token punctuation">}</span>
  <span class="token operator">&gt;</span>
    <span class="token punctuation">{</span>text<span class="token punctuation">}</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>li<span class="token operator">&gt;</span>
<span class="token punctuation">)</span>

Todo<span class="token punctuation">.</span>propTypes <span class="token operator">=</span> <span class="token punctuation">{</span>
  onClick<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>func<span class="token punctuation">.</span>isRequired<span class="token punctuation">,</span>
  completed<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>bool<span class="token punctuation">.</span>isRequired<span class="token punctuation">,</span>
  text<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>string<span class="token punctuation">.</span>isRequired
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> Todo
</code></pre>
<h4 id="-components-todolist-js"><code>components<span class="token operator">/</span>TodoList<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> PropTypes <span class="token punctuation">}</span> from <span class="token string">&apos;react&apos;</span>
<span class="token keyword">import</span> Todo from <span class="token string">&apos;./Todo&apos;</span>

<span class="token keyword">const</span> TodoList <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> todos<span class="token punctuation">,</span> onTodoClick <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>ul<span class="token operator">&gt;</span>
    <span class="token punctuation">{</span>todos<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>todo <span class="token operator">=</span><span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span>Todo
        key<span class="token operator">=</span><span class="token punctuation">{</span>todo<span class="token punctuation">.</span>id<span class="token punctuation">}</span>
        <span class="token punctuation">{</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>todo<span class="token punctuation">}</span>
        onClick<span class="token operator">=</span><span class="token punctuation">{</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">onTodoClick</span><span class="token punctuation">(</span>todo<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">}</span>
      <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token punctuation">)</span><span class="token punctuation">}</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>ul<span class="token operator">&gt;</span>
<span class="token punctuation">)</span>

TodoList<span class="token punctuation">.</span>propTypes <span class="token operator">=</span> <span class="token punctuation">{</span>
  todos<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span><span class="token function">arrayOf</span><span class="token punctuation">(</span>PropTypes<span class="token punctuation">.</span><span class="token function">shape</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    id<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>number<span class="token punctuation">.</span>isRequired<span class="token punctuation">,</span>
    completed<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>bool<span class="token punctuation">.</span>isRequired<span class="token punctuation">,</span>
    text<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>string<span class="token punctuation">.</span>isRequired
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>isRequired<span class="token punctuation">)</span><span class="token punctuation">.</span>isRequired<span class="token punctuation">,</span>
  onTodoClick<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>func<span class="token punctuation">.</span>isRequired
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> TodoList
</code></pre>
<h4 id="-components-link-js"><code>components<span class="token operator">/</span>Link<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React<span class="token punctuation">,</span> <span class="token punctuation">{</span> PropTypes <span class="token punctuation">}</span> from <span class="token string">&apos;react&apos;</span>

<span class="token keyword">const</span> Link <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> active<span class="token punctuation">,</span> children<span class="token punctuation">,</span> onClick <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>active<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token operator">&lt;</span>span<span class="token operator">&gt;</span><span class="token punctuation">{</span>children<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>span<span class="token operator">&gt;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">return</span> <span class="token punctuation">(</span>
    <span class="token operator">&lt;</span>a href<span class="token operator">=</span><span class="token string">&quot;#&quot;</span>
       onClick<span class="token operator">=</span><span class="token punctuation">{</span>e <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
         e<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
         <span class="token function">onClick</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
       <span class="token punctuation">}</span><span class="token punctuation">}</span>
    <span class="token operator">&gt;</span>
      <span class="token punctuation">{</span>children<span class="token punctuation">}</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>a<span class="token operator">&gt;</span>
  <span class="token punctuation">)</span>
<span class="token punctuation">}</span>

Link<span class="token punctuation">.</span>propTypes <span class="token operator">=</span> <span class="token punctuation">{</span>
  active<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>bool<span class="token punctuation">.</span>isRequired<span class="token punctuation">,</span>
  children<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>node<span class="token punctuation">.</span>isRequired<span class="token punctuation">,</span>
  onClick<span class="token punctuation">:</span> PropTypes<span class="token punctuation">.</span>func<span class="token punctuation">.</span>isRequired
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> Link
</code></pre>
<h4 id="-components-footer-js"><code>components<span class="token operator">/</span>Footer<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span>
<span class="token keyword">import</span> FilterLink from <span class="token string">&apos;../containers/FilterLink&apos;</span>

<span class="token keyword">const</span> Footer <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>p<span class="token operator">&gt;</span>
    Show<span class="token punctuation">:</span>
    <span class="token punctuation">{</span><span class="token string">&quot; &quot;</span><span class="token punctuation">}</span>
    <span class="token operator">&lt;</span>FilterLink filter<span class="token operator">=</span><span class="token string">&quot;SHOW_ALL&quot;</span><span class="token operator">&gt;</span>
      All
    <span class="token operator">&lt;</span><span class="token operator">/</span>FilterLink<span class="token operator">&gt;</span>
    <span class="token punctuation">{</span><span class="token string">&quot;, &quot;</span><span class="token punctuation">}</span>
    <span class="token operator">&lt;</span>FilterLink filter<span class="token operator">=</span><span class="token string">&quot;SHOW_ACTIVE&quot;</span><span class="token operator">&gt;</span>
      Active
    <span class="token operator">&lt;</span><span class="token operator">/</span>FilterLink<span class="token operator">&gt;</span>
    <span class="token punctuation">{</span><span class="token string">&quot;, &quot;</span><span class="token punctuation">}</span>
    <span class="token operator">&lt;</span>FilterLink filter<span class="token operator">=</span><span class="token string">&quot;SHOW_COMPLETED&quot;</span><span class="token operator">&gt;</span>
      Completed
    <span class="token operator">&lt;</span><span class="token operator">/</span>FilterLink<span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>p<span class="token operator">&gt;</span>
<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> Footer
</code></pre>
<h4 id="-components-app-js"><code>components<span class="token operator">/</span>App<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span>
<span class="token keyword">import</span> Footer from <span class="token string">&apos;./Footer&apos;</span>
<span class="token keyword">import</span> AddTodo from <span class="token string">&apos;../containers/AddTodo&apos;</span>
<span class="token keyword">import</span> VisibleTodoList from <span class="token string">&apos;../containers/VisibleTodoList&apos;</span>

<span class="token keyword">const</span> App <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>div<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>AddTodo <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>VisibleTodoList <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>Footer <span class="token operator">/</span><span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> App
</code></pre>
<h3 id="%E5%AE%B9%E5%99%A8%E7%BB%84%E4%BB%B6">&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;</h3>
<p>&#x73B0;&#x5728;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E9B;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x628A;&#x8FD9;&#x4E9B;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x548C; Redux &#x5173;&#x8054;&#x8D77;&#x6765;&#x3002;&#x6280;&#x672F;&#x4E0A;&#x8BB2;&#xFF0C;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x5C31;&#x662F;&#x4F7F;&#x7528; <a href="../api/Store.html#subscribe"><code>store<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a> &#x4ECE; Redux state &#x6811;&#x4E2D;&#x8BFB;&#x53D6;&#x90E8;&#x5206;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x901A;&#x8FC7; props &#x6765;&#x628A;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x63D0;&#x4F9B;&#x7ED9;&#x8981;&#x6E32;&#x67D3;&#x7684;&#x7EC4;&#x4EF6;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x624B;&#x5DE5;&#x6765;&#x5F00;&#x53D1;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#xFF0C;&#x4F46;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x4F7F;&#x7528; React Redux &#x5E93;&#x7684; <a href="https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options" target="_blank"><code><span class="token function">connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a> &#x65B9;&#x6CD5;&#x6765;&#x751F;&#x6210;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x505A;&#x4E86;&#x6027;&#x80FD;&#x4F18;&#x5316;&#x6765;&#x907F;&#x514D;&#x5F88;&#x591A;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x91CD;&#x590D;&#x6E32;&#x67D3;&#x3002;&#xFF08;&#x8FD9;&#x6837;&#x4F60;&#x5C31;&#x4E0D;&#x5FC5;&#x4E3A;&#x4E86;&#x6027;&#x80FD;&#x800C;&#x624B;&#x52A8;&#x5B9E;&#x73B0; <a href="https://facebook.github.io/react/docs/advanced-performance.html" target="_blank">React &#x6027;&#x80FD;&#x4F18;&#x5316;&#x5EFA;&#x8BAE;</a> &#x4E2D;&#x7684; <code>shouldComponentUpdate</code> &#x65B9;&#x6CD5;&#x3002;&#xFF09;</p>
<p>&#x4F7F;&#x7528; <code><span class="token function">connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x524D;&#xFF0C;&#x9700;&#x8981;&#x5148;&#x5B9A;&#x4E49; <code>mapStateToProps</code> &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x6307;&#x5B9A;&#x5982;&#x4F55;&#x628A;&#x5F53;&#x524D; Redux store state &#x6620;&#x5C04;&#x5230;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x7684; props &#x4E2D;&#x3002;&#x4F8B;&#x5982;&#xFF0C;<code>VisibleTodoList</code> &#x9700;&#x8981;&#x8BA1;&#x7B97;&#x4F20;&#x5230; <code>TodoList</code> &#x4E2D;&#x7684; <code>todos</code>&#xFF0C;&#x6240;&#x4EE5;&#x5B9A;&#x4E49;&#x4E86;&#x6839;&#x636E; <code>state<span class="token punctuation">.</span>visibilityFilter</code> &#x6765;&#x8FC7;&#x6EE4; <code>state<span class="token punctuation">.</span>todos</code> &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x5728; <code>mapStateToProps</code> &#x4E2D;&#x4F7F;&#x7528;&#x3002;</p>
<pre><code class="lang-js"><span class="token keyword">const</span> getVisibleTodos <span class="token operator">=</span> <span class="token punctuation">(</span>todos<span class="token punctuation">,</span> filter<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>filter<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">&apos;SHOW_ALL&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> todos
    <span class="token keyword">case</span> <span class="token string">&apos;SHOW_COMPLETED&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> todos<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">&gt;</span> t<span class="token punctuation">.</span>completed<span class="token punctuation">)</span>
    <span class="token keyword">case</span> <span class="token string">&apos;SHOW_ACTIVE&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> todos<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token operator">!</span>t<span class="token punctuation">.</span>completed<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> mapStateToProps <span class="token operator">=</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    todos<span class="token punctuation">:</span> <span class="token function">getVisibleTodos</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> state<span class="token punctuation">.</span>visibilityFilter<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x9664;&#x4E86;&#x8BFB;&#x53D6; state&#xFF0C;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x8FD8;&#x80FD;&#x5206;&#x53D1; action&#x3002;&#x7C7B;&#x4F3C;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49; <code><span class="token function">mapDispatchToProps</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x65B9;&#x6CD5;&#x63A5;&#x6536; <a href="../api/Store.html#dispatch"><code><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a> &#x65B9;&#x6CD5;&#x5E76;&#x8FD4;&#x56DE;&#x671F;&#x671B;&#x6CE8;&#x5165;&#x5230;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x7684; props &#x4E2D;&#x7684;&#x56DE;&#x8C03;&#x65B9;&#x6CD5;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B; <code>VisibleTodoList</code> &#x5411; <code>TodoList</code> &#x7EC4;&#x4EF6;&#x4E2D;&#x6CE8;&#x5165;&#x4E00;&#x4E2A;&#x53EB; <code>onTodoClick</code> &#x7684; props &#x4E2D;&#xFF0C;&#x8FD8;&#x5E0C;&#x671B; <code>onTodoClick</code> &#x80FD;&#x5206;&#x53D1; <code>TOGGLE_TODO</code> &#x8FD9;&#x4E2A; action&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">const</span> mapDispatchToProps <span class="token operator">=</span> <span class="token punctuation">(</span>dispatch<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    onTodoClick<span class="token punctuation">:</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">toggleTodo</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x4F7F;&#x7528; <code><span class="token function">connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x521B;&#x5EFA; <code>VisibleTodoList</code>&#xFF0C;&#x5E76;&#x4F20;&#x5165;&#x8FD9;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> connect <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span>

<span class="token keyword">const</span> VisibleTodoList <span class="token operator">=</span> <span class="token function">connect</span><span class="token punctuation">(</span>
  mapStateToProps<span class="token punctuation">,</span>
  mapDispatchToProps
<span class="token punctuation">)</span><span class="token punctuation">(</span>TodoList<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> VisibleTodoList
</code></pre>
<p>&#x8FD9;&#x5C31;&#x662F; React Redux API &#x7684;&#x57FA;&#x7840;&#xFF0C;&#x4F46;&#x8FD8;&#x6F0F;&#x4E86;&#x4E00;&#x4E9B;&#x5FEB;&#x6377;&#x6280;&#x5DE7;&#x548C;&#x5F3A;&#x5927;&#x7684;&#x914D;&#x7F6E;&#x3002;&#x5EFA;&#x8BAE;&#x4F60;&#x4ED4;&#x7EC6;&#x5B66;&#x4E60; <a href="https://github.com/reactjs/react-redux" target="_blank">&#x5B83;&#x7684;&#x6587;&#x6863;</a>&#x3002;&#x5982;&#x679C;&#x4F60;&#x62C5;&#x5FC3; <code>mapStateToProps</code> &#x521B;&#x5EFA;&#x65B0;&#x5BF9;&#x8C61;&#x592A;&#x8FC7;&#x9891;&#x7E41;&#xFF0C;&#x53EF;&#x4EE5;&#x5B66;&#x4E60;&#x5982;&#x4F55;&#x4F7F;&#x7528; <a href="https://github.com/reactjs/reselect" target="_blank">reselect</a> &#x6765; <a href="../recipes/ComputingDerivedData.html">&#x8BA1;&#x7B97;&#x884D;&#x751F;&#x6570;&#x636E;</a>&#x3002;</p>
<p>&#x5176;&#x5B83;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<h4 id="-containers-filterlink-js"><code>containers<span class="token operator">/</span>FilterLink<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> connect <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> setVisibilityFilter <span class="token punctuation">}</span> from <span class="token string">&apos;../actions&apos;</span>
<span class="token keyword">import</span> Link from <span class="token string">&apos;../components/Link&apos;</span>

<span class="token keyword">const</span> mapStateToProps <span class="token operator">=</span> <span class="token punctuation">(</span>state<span class="token punctuation">,</span> ownProps<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    active<span class="token punctuation">:</span> ownProps<span class="token punctuation">.</span>filter <span class="token operator">===</span> state<span class="token punctuation">.</span>visibilityFilter
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> mapDispatchToProps <span class="token operator">=</span> <span class="token punctuation">(</span>dispatch<span class="token punctuation">,</span> ownProps<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    onClick<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">setVisibilityFilter</span><span class="token punctuation">(</span>ownProps<span class="token punctuation">.</span>filter<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> FilterLink <span class="token operator">=</span> <span class="token function">connect</span><span class="token punctuation">(</span>
  mapStateToProps<span class="token punctuation">,</span>
  mapDispatchToProps
<span class="token punctuation">)</span><span class="token punctuation">(</span>Link<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> FilterLink
</code></pre>
<h4 id="-containers-visibletodolist-js"><code>containers<span class="token operator">/</span>VisibleTodoList<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> connect <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> toggleTodo <span class="token punctuation">}</span> from <span class="token string">&apos;../actions&apos;</span>
<span class="token keyword">import</span> TodoList from <span class="token string">&apos;../components/TodoList&apos;</span>

<span class="token keyword">const</span> getVisibleTodos <span class="token operator">=</span> <span class="token punctuation">(</span>todos<span class="token punctuation">,</span> filter<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>filter<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">&apos;SHOW_ALL&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> todos
    <span class="token keyword">case</span> <span class="token string">&apos;SHOW_COMPLETED&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> todos<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">&gt;</span> t<span class="token punctuation">.</span>completed<span class="token punctuation">)</span>
    <span class="token keyword">case</span> <span class="token string">&apos;SHOW_ACTIVE&apos;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> todos<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>t <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token operator">!</span>t<span class="token punctuation">.</span>completed<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> mapStateToProps <span class="token operator">=</span> <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    todos<span class="token punctuation">:</span> <span class="token function">getVisibleTodos</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> state<span class="token punctuation">.</span>visibilityFilter<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> mapDispatchToProps <span class="token operator">=</span> <span class="token punctuation">(</span>dispatch<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">{</span>
    onTodoClick<span class="token punctuation">:</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
      <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">toggleTodo</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> VisibleTodoList <span class="token operator">=</span> <span class="token function">connect</span><span class="token punctuation">(</span>
  mapStateToProps<span class="token punctuation">,</span>
  mapDispatchToProps
<span class="token punctuation">)</span><span class="token punctuation">(</span>TodoList<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> VisibleTodoList
</code></pre>
<h3 id="%E5%85%B6%E5%AE%83%E7%BB%84%E4%BB%B6">&#x5176;&#x5B83;&#x7EC4;&#x4EF6;</h3>
<h4 id="-containers-addtodo-js"><code>containers<span class="token operator">/</span>AddTodo<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> connect <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> addTodo <span class="token punctuation">}</span> from <span class="token string">&apos;../actions&apos;</span>

<span class="token keyword">let</span> AddTodo <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">{</span> dispatch <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> input

  <span class="token keyword">return</span> <span class="token punctuation">(</span>
    <span class="token operator">&lt;</span>div<span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span>form onSubmit<span class="token operator">=</span><span class="token punctuation">{</span>e <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">preventDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>input<span class="token punctuation">.</span>value<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">return</span>
        <span class="token punctuation">}</span>
        <span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">addTodo</span><span class="token punctuation">(</span>input<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">)</span>
        input<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token string">&apos;&apos;</span>
      <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>input ref<span class="token operator">=</span><span class="token punctuation">{</span>node <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
          input <span class="token operator">=</span> node
        <span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token operator">/</span><span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>button type<span class="token operator">=</span><span class="token string">&quot;submit&quot;</span><span class="token operator">&gt;</span>
          Add Todo
        <span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
  <span class="token punctuation">)</span>
<span class="token punctuation">}</span>
AddTodo <span class="token operator">=</span> <span class="token function">connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>AddTodo<span class="token punctuation">)</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> AddTodo
</code></pre>
<h2 id="%E4%BC%A0%E5%85%A5-store">&#x4F20;&#x5165; Store</h2>
<p>&#x6240;&#x6709;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x90FD;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; Redux store&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x76D1;&#x542C;&#x5B83;&#x3002;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x628A;&#x5B83;&#x4EE5; props &#x7684;&#x5F62;&#x5F0F;&#x4F20;&#x5165;&#x5230;&#x6240;&#x6709;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x4E2D;&#x3002;&#x4F46;&#x8FD9;&#x592A;&#x9EBB;&#x70E6;&#x4E86;&#xFF0C;&#x56E0;&#x4E3A;&#x5FC5;&#x987B;&#x8981;&#x7528; <code>store</code> &#x628A;&#x5C55;&#x793A;&#x7EC4;&#x4EF6;&#x5305;&#x88F9;&#x4E00;&#x5C42;&#xFF0C;&#x4EC5;&#x4EC5;&#x662F;&#x56E0;&#x4E3A;&#x6070;&#x597D;&#x5728;&#x7EC4;&#x4EF6;&#x6811;&#x4E2D;&#x6E32;&#x67D3;&#x4E86;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x3002;</p>
<p>&#x5EFA;&#x8BAE;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x4F7F;&#x7528;&#x6307;&#x5B9A;&#x7684; React Redux &#x7EC4;&#x4EF6; <a href="https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store" target="_blank"><code><span class="token operator">&lt;</span>Provider<span class="token operator">&gt;</span></code></a> &#x6765; <a href="https://facebook.github.io/react/docs/context.html" target="_blank">&#x9B54;&#x6CD5;&#x822C;&#x7684;</a> &#x8BA9;&#x6240;&#x6709;&#x5BB9;&#x5668;&#x7EC4;&#x4EF6;&#x90FD;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; store&#xFF0C;&#x800C;&#x4E0D;&#x5FC5;&#x663E;&#x793A;&#x5730;&#x4F20;&#x9012;&#x5B83;&#x3002;&#x53EA;&#x9700;&#x8981;&#x5728;&#x6E32;&#x67D3;&#x6839;&#x7EC4;&#x4EF6;&#x65F6;&#x4F7F;&#x7528;&#x5373;&#x53EF;&#x3002;</p>
<h4 id="-index-js"><code>index<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> render <span class="token punctuation">}</span> from <span class="token string">&apos;react-dom&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Provider <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> createStore <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span>
<span class="token keyword">import</span> todoApp from <span class="token string">&apos;./reducers&apos;</span>
<span class="token keyword">import</span> App from <span class="token string">&apos;./components/App&apos;</span>

<span class="token keyword">let</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>todoApp<span class="token punctuation">)</span>

<span class="token function">render</span><span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>Provider store<span class="token operator">=</span><span class="token punctuation">{</span>store<span class="token punctuation">}</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>App <span class="token operator">/</span><span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>Provider<span class="token operator">&gt;</span><span class="token punctuation">,</span>
  document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&apos;root&apos;</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span>
</code></pre>
<h2 id="%E4%B8%8B%E4%B8%80%E6%AD%A5">&#x4E0B;&#x4E00;&#x6B65;</h2>
<p>&#x53C2;&#x7167; <a href="ExampleTodoList.html">&#x672C;&#x5B8C;&#x6574;&#x793A;&#x4F8B;</a> &#x6765;&#x6DF1;&#x5316;&#x7406;&#x89E3;&#x3002;&#x7136;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x8DF3;&#x5230; <a href="../advanced/index.html">&#x9AD8;&#x7EA7;&#x6559;&#x7A0B;</a> &#x5B66;&#x4E60;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x548C;&#x8DEF;&#x7531;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../docs/basics/DataFlow.html" class="navigation navigation-prev " aria-label="Previous page: 数据流"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../docs/basics/ExampleTodoList.html" class="navigation navigation-next " aria-label="Next page: 示例：Todo List"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-ga/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"edit-link":{"base":"https://github.com/camsong/redux-in-chinese/tree/master","label":"开始纠错"},"ga":{"token":"UA-66122997-1","configuration":"auto"}};
    gitbook.start(config);
});
</script>

        <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-66122997-1', 'auto');ga('send', 'pageview');</script>
    </body>
    
</html>
